<template>
  <div  class="wrapper">
    <div style="height:300px">
      <FormPanel  
        ref="from"
        :list="initForm"   
        :cols="3"
        :formType="value.type"
        :labelWidth="120"
        :isSubmitBtn="false"
        :collapse="false"
        @formChange="changeHandle"
      />
    </div>

    <BaseDialog
      :visible.sync="issearch"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="issearch2"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler2" />
    </BaseDialog>

    <div class="dialogBottmView">
      <el-button @click="closeDialog('closeDialog')">关 闭</el-button>
      <el-button :loading="loadingFlag" @click="saveDialog()" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { postSave } from '@/api/serviceass/batteryService/relationship';
import ownerSeacher from './ownerSeacher';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'servicessbatteryservicerelationshipinfo',
  mixins: [dictionary],
  components: {
    ownerSeacher
  },
  props:{
    value: {
      type: Object,
      data: Object,
      required: true
    },
  },
  data() {
    this.saveData=null;
    return {
      initForm : this.initFormFun(),
      id: '',
      type: this.value.type,
      issearch: false,
      issearch2: false,
      loadingFlag: false
    }
  },
  created() {
    this.init(this.value.data)
  },
  methods: {
    initFormFun() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'jxcode',
          placeholder: '请输入',
          disabled: true,
          initialValue: '',
          rules: [{
            required: true,
            message: '必填',
            trigger: 'blur'
          }]
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'jxname',
          placeholder: '请输入',
          initialValue: '',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun('1')}></el-button>
              </div>
            );
          },
          rules: [{
            required: true,
            message: '必填',
            trigger: 'blur'
          }]
        },
        {
          type: 'SELECT',
          label: '电池品牌',
          fieldName: 'fwdcpp',
          placeholder: '请选择',
          initialValue: "",
          itemList: this.createDictList('5014'),
          // rules: [{
          //   required: true,
          //   message: '必填',
          //   trigger: 'change'
          // }]
        },
        {
          type: 'INPUT',
          label: '对应经销商代码',
          fieldName: 'dycode',
          placeholder: '请输入',
          disabled: true,
          initialValue: '',
          rules: [{
            required: true,
            message: '必填',
            trigger: 'blur'
          }]
        },
        {
          type: 'INPUT',
          label: '对应经销商名称',
          fieldName: 'dyname',
          placeholder: '请输入',
          initialValue: '',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun('2')}></el-button>
              </div>
            );
          },
          rules: [{
            required: true,
            message: '必填',
            trigger: 'blur'
          }]
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'xtqybz',
          placeholder: '请选择',
          initialValue: "",
          itemList: this.createDictList('1003'),
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
      ]  
    },
    init(data) {
      if(this.type==='edit'){
        this.id = data.id
        this.initForm.forEach(x => {
          x.initialValue = data[x.fieldName];
        });
      }
    },
    changeHandle(val){
      this.saveData=val
    },
    seacherFun(val) {
      if (val === '1') {
        this.issearch = true
      } else if (val === '2') {
        this.issearch2 = true
      }
    },
    dealercloseHandler(val){
      if(val){
        console.info(val)
        this.initForm.forEach( x=> {
          if (x.fieldName === 'jxname') {
            x.initialValue = val.dealerName
          }
          if (x.fieldName === 'jxcode') {
            x.initialValue = val.dealerCode
          }
        })
      }
      this.issearch=false;
    },
    dealercloseHandler2(val){
      if(val){
        console.info(val)
        this.initForm.forEach( x=> {
          if (x.fieldName === 'dyname') {
            x.initialValue = val.dealerName
          }
          if (x.fieldName === 'dycode') {
            x.initialValue = val.dealerCode
          }
        })
      }
      this.issearch2=false;
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', {visible: false, type: type});
    },
    async saveDialog() {
      this.$refs.from.SUBMIT_FORM();
      if (this.saveData) {
        this.saveData.id = this.id
        if (this.saveData.jxcode === this.saveData.dycode) {
          this.$notify.error({ title: '提示信息', message: '经销商不能对应自己' || '操作失败!' });
          return false;
        }
        console.info(this.savedata)
        let res=await postSave(this.saveData)
        console.info(res)
        if (res.resultCode === 200) {
          this.loadingFlag = false;
          if (res.data.resultCode !== null && res.data.resultCode !== undefined && res.data.resultCode !== '') {
            this.$notify.error({ title: '提示信息', message: '该经销商已存在此对应关系' || '操作失败!' });
          } else {
            this.$notify.success({title: '提示信息',message:'保存成功!'});
            this.closeDialog('refresh')
          }
        } else {
          this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
        }
      }
    }
  }
}
</script>
